<template>
  <div class="mobile-menu">
    <!-- Logo 和首页链接 -->
    <div class="logo-container" @click="goToHome">
      <img
        class="logo"
        :src="logoSrc"
        alt="duibiao logo"
      />
      <span class="logo-text">对标网</span>
    </div>
    <div>
      <!-- 用户信息或登录按钮 -->
      <div class="user-section">
        <div v-if="isLoggedIn" class="user-info">
          <a-dropdown :trigger="['click']">
            <span class="ant-dropdown-link">
              <a-avatar :size="36">{{ username }}</a-avatar>
            </span>
            <template #overlay>
              <a-menu>
                <a-menu-item key="profile" @click="goToProfile">个人中心</a-menu-item>
                <a-menu-item key="logout" @click="logout">退出登录</a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
        <a-button v-else type="primary" @click="goToLogin" size="small">登录</a-button>

         <!-- 菜单按钮 -->
        <div class="menu-toggle" @click="toggleMenu">
          <menu-outlined />
        </div>
      </div>

     
    </div>
    <!-- 抽屉式菜单 -->
    <a-drawer
      placement="right"
      :closable="false"
      :visible="menuVisible"
      @close="toggleMenu"
      width="80%"
    >
      <template #handle>
        <div class="drawer-handle" @click="toggleMenu">
          <close-outlined />
        </div>
      </template>
      
      <div class="drawer-content">
        <a-menu
          mode="inline"
          :selected-keys="selectedKeys"
          @select="handleSelect"
        >
          <a-menu-item key="1" @click="goToHome">
            <home-outlined />
            <span>首页</span>
          </a-menu-item>
          <a-menu-item key="3" @click="goToCampatyList">
            <bank-outlined />
            <span>公司列表</span>
          </a-menu-item>
          <a-menu-item key="4" @click="goToSalarys">
            <dollar-outlined />
            <span>最新薪酬</span>
          </a-menu-item>
          <a-menu-item key="5" @click="goToWorkplaceroast">
            <message-outlined />
            <span>职场吐槽</span>
          </a-menu-item>
          <a-menu-item key="6" @click="goToFillInfo">
            <form-outlined />
            <span>信息添加</span>
          </a-menu-item>
          <a-menu-item key="7" @click="goToJobList">
            <team-outlined />
            <span>职位列表</span>
          </a-menu-item>
        </a-menu>
      </div>
    </a-drawer>
  </div>
</template>

<script>
import logoImage from '@/assets/images/duibiaologo.png'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  MenuOutlined, 
  CloseOutlined,
  HomeOutlined,
  BankOutlined,
  DollarOutlined,
  MessageOutlined,
  FormOutlined,
  TeamOutlined
} from '@ant-design/icons-vue'

export default {
  name: 'MenuTitle',
  components: {
    MenuOutlined,
    CloseOutlined,
    HomeOutlined,
    BankOutlined,
    DollarOutlined,
    MessageOutlined,
    FormOutlined,
    TeamOutlined
  },
  setup() {
    const router = useRouter()
    const selectedKeys = ref(['1'])
    const isLoggedIn = ref(false)
    const username = ref('')
    const menuVisible = ref(false)
    const logoSrc = ref(logoImage) // 修改为正确的路径

    // 检查用户是否已登录
    const checkLoginStatus = () => {
      const token = localStorage.getItem('ACCESS_TOKEN')
      isLoggedIn.value = !!token
      
      // 可以从localStorage或通过API获取用户名
      if (token) {
        const savedUsername = localStorage.getItem('USERNAME')
        username.value = savedUsername || '用户' // 如果没有保存用户名，则使用默认值'用户'
      }
    }

    // 切换菜单显示状态
    const toggleMenu = () => {
      menuVisible.value = !menuVisible.value
    }

    // 处理用户下拉菜单命令
    const handleUserCommand = async (command) => {
      if (command === 'logout') {
        try {
          // 这里应该调用登出API
          // await userApi.logout()
        } catch (error) {
          console.error('登出API调用失败:', error)
        } finally {
          // 清除本地token
          localStorage.removeItem('ACCESS_TOKEN')
          // 更新登录状态
          isLoggedIn.value = false
          // 跳转到登录页
          router.push('/login')
        }
      } else if (command === 'profile') {
        // 跳转到个人中心页面
        router.push({
          name: 'PersonalCenter',
          query: {
            username: username.value
          }
        })
      }
    }

    // 为下拉菜单项创建单独的方法
    const goToProfile = () => {
      handleUserCommand('profile')
      menuVisible.value = false
    }

    const logout = () => {
      handleUserCommand('logout')
      menuVisible.value = false
    }

    const handleSelect = (info) => {
      const { key, keyPath } = info
      if (key !== 'login') { // 排除登录按钮的select事件
        console.log(key, keyPath)
      }
      // 点击菜单项后关闭抽屉
      menuVisible.value = false
    }

    const goToHome = () => {
      router.push('/') // 跳转到首页
      menuVisible.value = false
    }
    
    const goToCampatyList = () => {
      router.push('/campatylist') // 跳转到公司列表页
      menuVisible.value = false
    }
    
    const goToSalarys = () => {
      router.push('/salarieslist') // 跳转到薪酬页
      menuVisible.value = false
    }

    const goToWorkplaceroast = () => {
      router.push('/roastlist') // 跳转到吐槽页
      menuVisible.value = false
    }

    const goToLogin = () => {
      router.push('/login') // 跳转到登录页
    }

    const goToFillInfo = () => {
      router.push('/fillinfo') // 跳转到信息添加页面
      menuVisible.value = false
    }
    
    const goToJobList = () => {
      router.push('/joblist') // 跳转到职位列表页面
      menuVisible.value = false
    }

    // 组件挂载时检查登录状态
    onMounted(() => {
      checkLoginStatus()
    })

    // 监听路由变化，更新登录状态
    // 确保 router 存在再添加 afterEach 钩子
    if (router && router.afterEach) {
      router.afterEach(() => {
        checkLoginStatus()
      })
    }

    return {
      selectedKeys,
      isLoggedIn,
      username,
      menuVisible,
      logoSrc,
      handleSelect,
      goToHome,
      goToCampatyList,
      goToSalarys,
      goToWorkplaceroast,
      goToLogin,
      goToFillInfo,
      goToJobList,
      goToProfile,
      logout,
      toggleMenu
    }
  }
}
</script>

<style scoped>
.mobile-menu {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 15px;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.logo-container {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.logo {
  width: 40px;
  height: 40px;
  margin-right: 10px;
}

.logo-text {
  font-size: 18px;
  font-weight: bold;
  color: #1890ff;
}

.user-section {
  display: flex;
  align-items: center;
}

.user-info {
  margin-right: 10px;
}

.menu-toggle {
  font-size: 24px;
  color: #1890ff;
  cursor: pointer;
  padding: 5px;
}

.drawer-handle {
  position: absolute;
  top: 50%;
  left: -20px;
  transform: translateY(-50%);
  width: 20px;
  height: 60px;
  background: #fff;
  border-radius: 4px 0 0 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: -2px 0 8px rgba(0, 0, 0, 0.15);
  cursor: pointer;
  color: #1890bff;
}

.drawer-content {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.drawer-content :deep(.ant-menu) {
  flex: 1;
  border-right: none;
}

.drawer-content :deep(.ant-menu-item) {
  height: 50px;
  font-size: 16px;
  display: flex;
  align-items: center;
}

.drawer-content :deep(.ant-menu-item .anticon) {
  font-size: 18px;
  margin-right: 12px;
}

@media (min-width: 768px) {
  .mobile-menu {
    padding: 10px 30px;
  }
  
  .logo {
    width: 50px;
    height: 50px;
  }
  
  .logo-text {
    font-size: 20px;
  }
}
</style>